﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Sdari.Api.DbModel;

internal partial class TokenServiceContext : DbContext
{
    public TokenServiceContext(DbContextOptions<TokenServiceContext> options)
        : base(options)
    {
    }

    public virtual DbSet<ApiConsumerAccess> ApiConsumerAccess { get; set; }

    public virtual DbSet<ApiInfo> ApiInfo { get; set; }

    public virtual DbSet<ApiWhitelist> ApiWhitelist { get; set; }

    public virtual DbSet<CallingLog> CallingLog { get; set; }

    public virtual DbSet<ConsumerInfo> ConsumerInfo { get; set; }

    public virtual DbSet<TokenInfo> TokenInfo { get; set; }

    public virtual DbSet<vwApiConsumerAccess> vwApiConsumerAccess { get; set; }

    public virtual DbSet<vwCallingLog> vwCallingLog { get; set; }

    public virtual DbSet<vwWhiteList> vwWhiteList { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApiConsumerAccess>(entity =>
        {
            entity.HasIndex(e => e.Uid, "UK_ApiConsumerAccess").IsUnique();

            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.HasOne(d => d.ApiU).WithMany(p => p.ApiConsumerAccess)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ApiUid)
                .HasConstraintName("FK_ApiConsumerAccess_ApiInfo");

            entity.HasOne(d => d.ConsumerU).WithMany(p => p.ApiConsumerAccess)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ConsumerUid)
                .HasConstraintName("FK_ApiConsumerAccess_ConsumerInfo");
        });

        modelBuilder.Entity<ApiInfo>(entity =>
        {
            entity.HasIndex(e => e.Uid, "UK_ApiInfo").IsUnique();

            entity.HasIndex(e => e.Address, "UK_ApiInfo_Address").IsUnique();

            entity.Property(e => e.Address)
                .IsRequired()
                .HasMaxLength(500)
                .IsUnicode(false)
                .HasComment("Api地址");
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description).HasMaxLength(50);
            entity.Property(e => e.Help)
                .HasMaxLength(500)
                .IsUnicode(false)
                .HasComment("Help Address");
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Name).HasMaxLength(50);
        });

        modelBuilder.Entity<ApiWhitelist>(entity =>
        {
            entity.HasIndex(e => e.Uid, "UK_ApiWhitelist").IsUnique();

            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.IsValid).HasComment("是否有效");
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Type)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Value)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.HasOne(d => d.ApiU).WithMany(p => p.ApiWhitelist)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ApiUid)
                .HasConstraintName("FK_ApiWhitelist_ApiInfo");
        });

        modelBuilder.Entity<CallingLog>(entity =>
        {
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.Ip).HasMaxLength(16);
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Result)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.HasOne(d => d.ApiU).WithMany(p => p.CallingLog)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ApiUid)
                .HasConstraintName("FK_CallingLog_ApiInfo");

            entity.HasOne(d => d.ConsumerU).WithMany(p => p.CallingLog)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ConsumerUid)
                .HasConstraintName("FK_CallingLog_ConsumerInfo");

            entity.HasOne(d => d.WhiteListU).WithMany(p => p.CallingLog)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.WhiteListUid)
                .HasConstraintName("FK_CallingLog_ApiWhitelist");
        });

        modelBuilder.Entity<ConsumerInfo>(entity =>
        {
            entity.HasIndex(e => e.ClientId, "IX_ConsumerInfo").IsUnique();

            entity.HasIndex(e => e.Uid, "UK_ConsumerInfo").IsUnique();

            entity.Property(e => e.ClientId)
                .IsRequired()
                .HasMaxLength(16)
                .IsUnicode(false);
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.Ipv4)
                .HasMaxLength(4)
                .IsFixedLength();
            entity.Property(e => e.Ipv6)
                .HasMaxLength(16)
                .IsFixedLength();
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Name).HasMaxLength(50);
            entity.Property(e => e.Secret)
                .IsRequired()
                .HasMaxLength(32)
                .IsUnicode(false);
        });

        modelBuilder.Entity<TokenInfo>(entity =>
        {
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.HasOne(d => d.ConsumerU).WithMany(p => p.TokenInfo)
                .HasPrincipalKey(p => p.Uid)
                .HasForeignKey(d => d.ConsumerUid)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_TokenInfo_ConsumerInfo");
        });

        modelBuilder.Entity<vwApiConsumerAccess>(entity =>
        {
            entity
                .HasNoKey()
                .ToView("vwApiConsumerAccess");

            entity.Property(e => e.ApiAddress)
                .HasMaxLength(500)
                .IsUnicode(false);
            entity.Property(e => e.ApiName).HasMaxLength(50);
            entity.Property(e => e.ConsumerName)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<vwCallingLog>(entity =>
        {
            entity
                .HasNoKey()
                .ToView("vwCallingLog");

            entity.Property(e => e.ApiName).HasMaxLength(50);
            entity.Property(e => e.ConsumerName)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Result)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.WhiteListType)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.WhiteListValue)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<vwWhiteList>(entity =>
        {
            entity
                .HasNoKey()
                .ToView("vwWhiteList");

            entity.Property(e => e.Address)
                .HasMaxLength(500)
                .IsUnicode(false);
            entity.Property(e => e.Creator)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Description)
                .HasMaxLength(10)
                .IsFixedLength();
            entity.Property(e => e.LastUpdater)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Name).HasMaxLength(50);
            entity.Property(e => e.Type)
                .HasMaxLength(50)
                .IsUnicode(false);
            entity.Property(e => e.Value)
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
